﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.Comm;

namespace VehicleMaintenance.BaseInfo
{
    public partial class BaseInfoForm : AddOrEditBaseForm
    {
        public BaseInfoForm()
        {
            InitializeComponent();

			ucFormInput1.InitializeControls(typeof(Models.BaseInfo));
        }

        
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                CarDBContext db = new CarDBContext();

                if (this.state == State.Edit)
                {
                    var baseInfo = db.BaseInfo.First(p => p.ID > 0);
                    ucFormInput1.GetValues(baseInfo);
                }
                else
                {
                    var baseInfo = db.BaseInfo.Create();
                    ucFormInput1.GetValues(baseInfo);

                    db.BaseInfo.Add(baseInfo);
                }

                int count = db.SaveChanges();

                this.ShowInfo("保存成功！");
				this.DialogResult = System.Windows.Forms.DialogResult.OK;

            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void BaseInfoForm_Load(object sender, EventArgs e)
        {
            try
            {
                CarDBContext db = new CarDBContext();
                VehicleMaintenance.Models.BaseInfo info = db.BaseInfo.FirstOrDefault(p => p.ID > 0);
                if (info == null)
                {
                    this.state = State.Add;

                    this.ClearData(this.panel1);
                }
                else
                {
                    this.state = State.Edit;

                    ucFormInput1.SetValues(info);
                }
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }
    }
}
